package life.majiang.community.controller;

import life.majiang.community.model.Tag;
import life.majiang.community.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping("/findAllTags")
    public Map<String,Object> findAllTags(HttpServletRequest request){
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            Object obj = request.getParameter("token")!=null?request.getParameter("token"):"";
            if(!"itanyStudy".equals(obj+"")){
                result.put("status",401);
                result.put("msg","未授权");
                return result;
            }
            result.put("status",200);
            result.put("data",tagService.findAllTags());
        }catch (Exception e){
            result.put("status",500);
            result.put("data",e.getMessage());
        }
        return result;
    }

    @RequestMapping("/addTag")
    public Map<String,Object> addTag(Tag tag, HttpServletRequest request){
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            Object obj = request.getParameter("token")!=null?request.getParameter("token"):"";
            if(!"itanyStudy".equals(obj+"")){
                result.put("status",401);
                result.put("msg","未授权");
                return result;
            }
            result.put("status",200);
            tagService.addTag(tag);
        }catch (Exception e){
            result.put("status",500);
            result.put("data",e.getMessage());
        }
        return result;
    }

    @RequestMapping("/modifyTag")
    public Map<String,Object> modifyTag(Tag tag,HttpServletRequest request){
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            Object obj = request.getParameter("token")!=null?request.getParameter("token"):"";
            if(!"itanyStudy".equals(obj+"")){
                result.put("status",401);
                result.put("msg","未授权");
                return result;
            }
            result.put("status",200);
            tagService.modifyTag(tag);
        }catch (Exception e){
            result.put("status",500);
            result.put("data",e.getMessage());
        }
        return result;
    }


}
